package com.metaera.app.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.metaera.app.Constants;
import com.metaera.app.R;
import com.metaera.app.controller.presenter.LoginPresenter;
import com.metaera.app.controller.presenter.LoginVerPresenter;
import com.metaera.app.controller.view.BasicNetworkRequestView;
import com.metaera.app.controller.view.LoginView;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.UserDetailsResponse;
import com.metaera.app.ui.core.BaseActivity;
import com.metaera.app.utils.LifecycleCountDownTimer;
import com.metaera.app.utils.SoftKeyBoardUtils;
import com.metaera.app.utils.TextWatcherAdapter;

import androidx.annotation.NonNull;


public class VerifyCodeActivity extends BaseActivity {

    public static final int FORGET_TYPE = 1,
            AUTH_CODE_TYPE              = 0,
            BIND_WX_TYPE                = 2;

    public static Intent newInstance(Activity activity, CharSequence phoneNumber, CharSequence areaCode, int pageType) {
        Intent intent = new Intent(activity, VerifyCodeActivity.class);
        intent.putExtra(Constants.PHONE_NUMBER, phoneNumber.toString());
        intent.putExtra(Constants.EXTRA_AREA_CODE, areaCode.toString());
        intent.putExtra("PAGE_TYPE", pageType);
        return intent;
    }

    public static Intent newInstance(Activity activity, CharSequence phoneNumber, CharSequence areaCode, String token) {
        Intent intent = new Intent(activity, VerifyCodeActivity.class);
        intent.putExtra(Constants.PHONE_NUMBER, phoneNumber.toString());
        intent.putExtra(Constants.EXTRA_AREA_CODE, areaCode.toString());
        intent.putExtra("TOKEN", token);
        intent.putExtra("PAGE_TYPE", BIND_WX_TYPE);
        return intent;
    }


    @Override
    protected int getContentLayoutId() {

        return R.layout.activity_verify_code;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        // find
        TextView tvCodeTips = findViewById(R.id.tv_code_tips);
        TextView tvGetVerifyCode = findViewById(R.id.tv_get_verifyCode);
        View verifyCodeBack = findViewById(R.id.verify_code_back);
        EditText etVerifyCode = findViewById(R.id.et_verify_code);

        String phoneCode = getStringExtra(Constants.EXTRA_AREA_CODE);
        String phoneNumber = getStringExtra(Constants.PHONE_NUMBER);

        // network
        LoginVerPresenter presenter = new LoginVerPresenter();
        presenter.setModelAndView(new BasicNetworkRequestView<>(data -> {
            LifecycleCountDownTimer countDownTimer = new LifecycleCountDownTimer(
                    getLifecycle(),
                    tvGetVerifyCode,
                    getString(R.string.login_count_down_timer)) {
                @Override
                public void onFinish() {
                    super.onFinish();
                    tvGetVerifyCode.setTextColor(Color.parseColor("#3F51B5"));
                    tvGetVerifyCode.setText("点击发送");
                    tvGetVerifyCode.setOnClickListener(v -> {
                        start();
                        tvGetVerifyCode.setTextColor(Color.parseColor("#B0B0BA"));
                        presenter.sendSms(phoneCode, phoneNumber);
                    });
                }
            };

            countDownTimer.start();
        }));

        presenter.sendSms(phoneCode, phoneNumber);

        // init listener
        verifyCodeBack.setOnClickListener(v -> finish());
        etVerifyCode.addTextChangedListener(new TextWatcherAdapter() {

            @Override
            public void afterTextChanged(@NonNull Editable s) {

                if (s.length() == 6) {
                    int pageType = getIntent().getIntExtra("PAGE_TYPE", -1);
                    if (pageType == FORGET_TYPE) {
                        startActivity(
                                ResetPasswordActivity.newInstance(
                                        VerifyCodeActivity.this,
                                        pageType,
                                        getStringExtra(Constants.PHONE_NUMBER),
                                        getStringExtra(Constants.EXTRA_AREA_CODE),
                                        s
                                ));
                        return;
                    }

                    SoftKeyBoardUtils.hind(VerifyCodeActivity.this);

                    LoginPresenter presenter = new LoginPresenter();

                    presenter.setModelAndView(
                            new BasicNetworkRequestView<>(
                                    new LoginView(VerifyCodeActivity.this)
                            )
                    );

                    if (pageType == BIND_WX_TYPE) {

                        presenter.bind(
                                getStringExtra(Constants.PHONE_NUMBER),
                                s,
                                getStringExtra(Constants.EXTRA_AREA_CODE),
                                getIntent().getStringExtra("TOKEN")
                        );

                        return;
                    }

                    // 验证码登录
                    presenter.ver(
                            getStringExtra(Constants.PHONE_NUMBER),
                            s,
                            getStringExtra(Constants.EXTRA_AREA_CODE)
                    );
                }
            }

        });
    }

}
